package LoginManager;

import java.sql.*;
import DBManager.DBConnection;

/**
 *
 * @author Alex
 */
public class LoginManager {
     public boolean LoginUser(String username, String password)
    {
        boolean result = false;

        try{
            //get connection
            DBConnection db = new DBConnection();
            Connection conn = db.getConnection();

            //Construct SQL Query
           PreparedStatement pstmt= conn.prepareStatement("SELECT * FROM customer WHERE username = ? AND password = ?");

            //append values for the parameters
            int i=1;
             pstmt.setString(i++, username);
              pstmt.setString(i++, password);



            //execute SQL statement
        ResultSet rs= pstmt.executeQuery();

            //check if there is a data inside rs

 while(rs.next())
 {
 result=true;
 }
 conn.close();
        }
        catch(SQLException ex){ex.printStackTrace();}

        return result;
    }
     
     

    
     public String getInfo(String username, String needed)
    {
        String resultuser="";
        try{
            DBConnection db = new DBConnection();
            Connection conn = db.getConnection();

            String query = "select *  from manager where username=\"" + username + "\"";
            Statement  stmt = conn.createStatement();
            ResultSet rset = stmt.executeQuery(query);

             while (rset.next())
             {

                    resultuser=rset.getString(needed);

             }

             }
        catch(SQLException ex){ 
            ex.printStackTrace();
        }
         
        return resultuser;
    }
     
     
   public void updateLastLog(String username)
    {
        try{
            DBConnection db = new DBConnection();
            Connection conn = db.getConnection();

            String  query = "update user set lastlogin = DATE_FORMAT(NOW(), '%y-%m-%d %T') where username = ?";
            PreparedStatement pstmt = conn.prepareStatement(query);
            pstmt.setString(1, username);
            pstmt.executeUpdate();
            //UPDATE user SET lastlogin = DATE_FORMAT(NOW(), '%y-%m-%d %T') WHERE username = 'loner50';
        }
        catch(SQLException ex){ 
            ex.printStackTrace();
        }

    }
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     

      
         public String getUsername()
    {

        String result= "";

        try{
            //get connection
            DBConnection db = new DBConnection();
            Connection conn = db.getConnection();

            //Construct SQL Query
           PreparedStatement pstmt= conn.prepareStatement("SELECT * FROM login");

            //append values for the parameters






            //execute SQL statement
        ResultSet rs= pstmt.executeQuery();

            //check if there is a data inside rs

 while(rs.next())
 {
 result=rs.getString(2);
 }
 conn.close();
        }
        catch(SQLException ex){ex.printStackTrace();}

        return result;
    }

     
     
      public String getID(String username, String password)
    {

        String result= "";

        try{
            //get connection
            DBConnection db = new DBConnection();
            Connection conn = db.getConnection();

            //Construct SQL Query
           PreparedStatement pstmt= conn.prepareStatement("SELECT * FROM haha ");

            //append values for the parameters






            //execute SQL statement
        ResultSet rs= pstmt.executeQuery();

            //check if there is a data inside rs

 while(rs.next())
 {
 result=rs.getString(2);
 }
 conn.close();
        }
        catch(SQLException ex){ex.printStackTrace();}

        return result;
    }
     

      
}